<?php

namespace app\admin\controller\prl;

use app\common\controller\Backend;

/**
 * 视图管理
 *
 * @icon fa fa-circle-o
 */
class Views extends Backend
{
    
    /**
     * Views模型对象
     * @var \app\admin\model\prl\Views
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\prl\Views;
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    protected $beforeActionList = [
        'getViewsListFile'  =>  ['only'=>'add,edit'],
    ];

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    public function selectpage()
    {
        //主键
        $primarykey = $this->request->request("keyField");
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        //当前页
        $page = $this->request->request("pageNumber");
        //分页大小
        $pagesize = $this->request->request("pageSize");

        //如果有primaryvalue,说明当前是初始化传值
        if ($primaryvalue !== null) {
            $where = [$primarykey => ['in', $primaryvalue]];
            $list = $this->model
            ->page($page, $pagesize)
            ->where($where)
            ->select();
            $total = $this->model
            ->where($where)
            ->count();
        }else{
            $where = [];
            $type = $this->request->param('type', null);
            if($type != null){
                $where['type'] = $type;
            }
            $list = $this->model
            ->where($where)
            ->page($page, $pagesize)
            ->select();
            $list = collection($list)->toArray();
            $total = $this->model->where($where)->count();
        }

        return json(['list' => $list, 'total' => $total]);
    }

    /**
     * 获取视图文件列表
     */
    protected function getViewsListFile()
    {
        $list = $this->model->getFileList();
        $this->view->assign('tplList', $list);
    }
}
